<?php
require_once("xajax.inc.php");
class myXajaxResponse extends xajaxResponse  
	{
	function addCreateOptions($taget_field, $options, $key, $value) 
		{
		$this->addScript("document.getElementById('".$taget_field."').length=1");
		if (sizeof($options) >0) 
			{
			foreach ($options as $option) 
				{
				$this->addScript("addOption('".$taget_field."','".$option[$key]."','".$option[$value]."');");
				}
			}
  		}
	}
$xajax = new xajax();
$xajax->registerFunction("getGradeDetails");
$xajax->registerFunction("getSelectMenuOptions");
$xajax->registerFunction("getPlanReference");

$xajax->processRequests();
// Also add the Xajax subdirectory name as an argument if you have the Xajax files, etc. within a subdirectory
$xajax->printJavascript();

function getGradeDetails($group_grade_id,$source_field,$product_id,$colour_id,$batch_quantity,$location_id,$account_id,$raw_colour_id,$stock_type_id,$stock_category_id)
	{
	include("global_files/tables.inc");
	global $db;
	$form=new cForm();
	$module_id_array=array('gen');
	$page_locale_array=fetchLocaleStrings($module_id_array);
	
	$account_id=$account_id?$account_id:'0';
	$raw_colour_id=$raw_colour_id?$raw_colour_id:'0';
	$stock_type_id=$stock_type_id?$stock_type_id:'0';
	$stock_category_id=$stock_category_id?$stock_category_id:'0';
	
	$db->query="SELECT product_desc as raw_material_type, raw_material_type_id, selection_value, sms.selection_id, quantity
	FROM $product_grade_relation_table pgr
	JOIN $master_product_101_table mp ON mp.product_id = pgr.raw_material_type_id
	JOIN $sys_master_selection_106_table sms ON sms.selection_id=pgr.selection_id
	WHERE pgr.grade_group_id=$group_grade_id AND pgr.product_id='$product_id' AND pgr.colour_id='$colour_id'
	ORDER BY product_desc, selection_value;";
	$db->runQuery();
	$details_array=$db->returnArrays();
	
	$db->query="SELECT product_id, product_desc FROM $master_product_101_table ORDER BY product_desc;";
	$db->runQuery();
	$raw_material_type_id_array=$db->returnArrays();
	
	$db->query="SELECT selection_id, selection_value FROM $sys_master_selection_106_table ORDER BY selection_value;";
	$db->runQuery();
	$selection_id_array=$db->returnArrays();
	$error=0;
	for($i=0;$i<count($details_array);$i++)
		{
		$db->query="SELECT round(coalesce(balance_quantity[1]-allocated_quantity[1],0)) as bal_to_allocate 
		FROM $stk_meta_product_balance_101_table WHERE product_id='{$details_array[$i][raw_material_type_id]}' AND selection_id='{$details_array[$i][selection_id]}' AND specifications[1]='$location_id' AND specifications[2]='$account_id' AND specifications[3]='$raw_colour_id' AND specifications[4]='$stock_type_id' AND specifications[5]='$stock_category_id';";
		$db->runQuery();
		$allocated_quantity_array=array();
		$allocated_quantity_array=$db->returnArrays();
		
		$form_data[$i]["raw_material_type_id"]=$details_array[$i]["raw_material_type"];
		$form_data[$i]["selection_value"]=$details_array[$i]["selection_value"];
		$form_data[$i]["%"]=$details_array[$i]["quantity"];
		$allocated_quantity=0;
		$allocated_quantity=round($batch_quantity*$details_array[$i]["quantity"]/100);
		$bal_to_allocate=$allocated_quantity_array[0]["bal_to_allocate"];
		if($allocated_quantity<=$bal_to_allocate)
			{
			$form_data[$i]["quantity"]=$allocated_quantity;
			}
		else
			{
			$form_data[$i]["quantity"]="<font color=red>".$allocated_quantity."</div>";
			$error++;
			}
		$form_data[$i]["stock_quantity"]=$bal_to_allocate;
		}
	$column_properties[0]["raw_material_type_id"]="text-align:left;";
	$column_properties[0]["selection_value"]="text-align:left;";
	$column_properties[0]["%"]="text-align:right;";
	$column_properties[0]["quantity"]="text-align:right;";
	$column_properties[0]["stock_quantity"]="text-align:right;";
	$column_properties=replaceToLocaleString($column_properties,$page_locale_array);
	
	$value=createHorizontalTable(replaceToLocaleString($form_data,$page_locale_array),"50%","{$page_locale_array[HEADING][grade_details]}",$column_properties[0]);
	$value.=$form->generateInputTag("submit",array("name"=>"raw_planning","value"=>"Save","accesskey"=>"3","onclick"=>"if($error>0){alert('Invalid Stock Quantity');return false;}else{return true;}"));
	
	$objResponse = new xajaxResponse();
	$objResponse->addAssign($source_field,"innerHTML", $value);
	return $objResponse;
	}

function getSelectMenuOptions($selected_id,$taget_field,$key,$value,$query,$condition) 
	{
	global $db;
	$objResponse = new myXajaxResponse();
	
	$db->query="$query $condition='$selected_id';";
	$db->runQuery();
	$data_array=$db->returnArrays();
	$objResponse->addCreateOptions($taget_field, $data_array, $key, $value);
	return $objResponse->getXML();
	}
function getPlanReference($selected_id,$target_field)
	{
	include("global_files/tables.inc");
	global $db;
	
	list($order_id,$item_id)=explode('-',$selected_id);
	
	$db->query="SELECT internal_order_no FROM $order_details_items_table WHERE order_id=$order_id AND item_id=$item_id;";
	$db->runQuery();
	$order_no_array=$db->returnArrays();
	
	$db->query="SELECT count(*) as plan_reference FROM $sal_order_allocation_table WHERE order_id=$order_id AND item_id=$item_id;";
	$db->runQuery();
	$plan_id_count_array=$db->returnArrays();
	$plan_id_count=$plan_id_count_array[0]["plan_reference"]+1;
	$plan_reference=$order_no_array[0]["internal_order_no"]." - Plan $plan_id_count";
	
	$objResponse = new myXajaxResponse();
	$objResponse->addAssign($target_field,"value",$plan_reference);
	return $objResponse->getXML();
	}
?>
<script type="text/javascript">
function addOption(selectId, val, txt) 
	{
	var objOption = new Option(txt, val);
	document.getElementById(selectId).options.add(objOption);
   	}
</script>